//
//  MakeStep2Layer.cpp
//  slushy
//
//  Created by wusonglin on 17/6/26.
//
//

#include "MakeStep2Layer.h"
#include "DecorationLayer.h"
#include "TouchMoveComponent.h"
#include "TouchNodeComponent.h"
IMPLEMENT_COCOS2DX_CLASS(MakeStep2Layer);
REGIST_SECENE(MakeStep2Layer)
bool MakeStep2Layer::init()
{
    if(BaseStepLayer::init()) {
        autoShowBtnName.clear();
        
        m_bIsTouch = false;
        
        _operate = StudioLayer::create("MakeStep2Layer.csb");
        this->addChild(_operate,1);
        
        _prompt = _operate->getNodeByName<Sprite*>("finger");
        
        auto hideNodes = {"corkscrew", "soda_down","bigcup", "corkscrew1", "bowlUp", "bowl"};
        for(auto name:hideNodes){
            _operate->getNodeByName(name)->setVisible(false);
            
        }

        
        
        return true;
    }
    return false;
}
void MakeStep2Layer::touchEnd(ui::Widget* widget)
{
    
}
void MakeStep2Layer::onEnterTransitionDidFinish()
{
    BaseMakeLayer::onEnterTransitionDidFinish();
    
    ActionHelper::delayFunc(2.0, this, [=](){
        
        
        this->showUIOne();
    });
}
//展示酒瓶  开瓶器
void MakeStep2Layer::showUIOne()
{
    
    //增加节点
//    auto cup = _operate->getNodeByName("cup");
    auto corkscrew = _operate->getNodeByName("corkscrew");
    auto soda_down = _operate->getNodeByName("soda_down");
    
    //杯子
//    auto cup_cover = Sprite::create("png/make2/cup_up.png");
//    cup_cover:setAnchorPoint(Vec2::ZERO);
//    cup->addChild(cup_cover);
//    
//    auto cup_shadow = Sprite::create("png/make2/cup_shadow.png");
//    cup_shadow:setAnchorPoint(Vec2::ZERO);
//    cup->addChild(cup_shadow, -1);
//
//    //开瓶器
    auto cup_shadow_shadow = Sprite::create("png/make2/corkscrew_shadow.png");
    cup_shadow_shadow->setAnchorPoint(Vec2::ZERO);

    corkscrew->addChild(cup_shadow_shadow, -1);

    //酒瓶
    auto soda_down_up = Sprite::create("png/make2/soda_down.png");
    soda_down_up->setAnchorPoint(Vec2::ZERO);
    soda_down->addChild(soda_down_up, 10);

    //具体的果汁  根据上一步得到
    auto soda_down_through = Sprite::create("png/make2/soda_up_blueberry.png");
    soda_down_through->setAnchorPoint(Vec2::ZERO);
    soda_down->addChild(soda_down_through, 10);

    auto soda_down_fruit = Sprite::create("png/make2/soda_blueberry0.png");
    soda_down_fruit->setAnchorPoint(Vec2::ZERO);
    soda_down->addChild(soda_down_fruit, 5);

    //瓶盖
    _lid = Sprite::create("png/make2/lid.png");
    _lid->setPosition(Vec2(60, 398));
    _lid:setTag(100);
    soda_down->addChild(_lid, 15);

    //酒瓶阴影
    auto soda_down_shadow = Sprite::create("png/make2/soda_shadow.png");
    soda_down_shadow->setAnchorPoint(Vec2::ZERO);
    soda_down->addChild(soda_down_shadow, -1);
    
    
    soda_down->setVisible(true);
    
    ActionHelper::showBezier(soda_down, soda_down->getPosition(), ActionHelper::ShowDirection::show_from_top, [=](){
    
        corkscrew->setVisible(true);
        ActionHelper::showBackInOut(corkscrew, corkscrew->getPosition(), ActionHelper::ShowDirection::show_from_right, [=](){
            
            //手指提示 拖动
            ActionHelper::runBezierMoveHideAction(_prompt, _operate->getPosinWord("corkscrew"), _operate->getPosinWord("limit"));
            auto touchMove = TouchMoveComponent::create();
            corkscrew->addComponent(touchMove);
            touchMove->setMoveType(MoveComponentType::kTypeBack);
            touchMove->setTarget(LQRect::create(CocosHelper::getNodeRectInWorld(_operate->getNodeByName("limit"))));

            touchMove->setBrintTop(true);
            touchMove->addListners(ComponentTouchEnd, [=](LQComponent*,OperateListner*){
                this->stopPrompt();
                //移动到目标位置
                this->corkscrewAction();
                corkscrew->removeComponent("TouchMoveComponent");
                corkscrew->setPosition(_operate->getNodeByName("corkscrew1")->getPosition());
            });
            
            
        });
    });
}

//开瓶器逻辑
void MakeStep2Layer::corkscrewAction()
{
    auto corkscrew = _operate->getNodeByName("corkscrew");
    auto soda_down = _operate->getNodeByName("soda_down");
    auto touchNode = TouchNodeComponent::create();
    touchNode->setSwallowTouches(false);
    corkscrew->addComponent(touchNode);
    ActionHelper::runMoveHideAction(_prompt, _operate->getPosinWord("limit2"), _operate->getPosinWord("limit1"));
    
    static int indexNum = 0;
    static int index = 0;
    touchNode->addListners(ComponentTouchMove, [=](LQComponent* component,OperateListner* lis){
        this->stopPrompt();
        log("111111111");
        indexNum++;
        if (indexNum % 10 == 0) {
            touchNode->setEnabled(false);
            
            corkscrew->runAction(Sequence::create(MoveBy::create(0.4,Vec2(0, -5)),MoveBy::create(0.4,Vec2(0, 5)), NULL));
            soda_down->runAction(Sequence::create(ActionHelper::createShakeAction(5,0.2),
                                                  CallFunc::create([=](){
                
                touchNode->setEnabled(true);
                index++;
                if(index < 2)
                    ActionHelper::runMoveHideAction(_prompt, _operate->getPosinWord("limit2"), _operate->getPosinWord("limit1"));
                
                if(index == 3){
                    touchNode->setEnabled(false);
                    
                    corkscrew->runAction(Sequence::create(Spawn::create(MoveBy::create(1.0, Vec2(1000, 0)),RotateBy::create(0.5,360), NULL),CallFunc::create([=](){
                    
                        _lid->runAction(Sequence::create(MoveBy::create(0.5, Vec2(1000, 0)),RemoveSelf::create(),CallFunc::create([=](){
                        
                            soda_down->runAction(Sequence::create(MoveTo::create(0.5, _operate->getNodeByName("bigcup")->getPosition()),CallFunc::create([=](){
                                
                                soda_down->setVisible(false);
//                                _operate->getNodeByName("bigcup")->setVisible(true);
                                
                                auto bigcup = _operate->getNodeByName("bigcup");
                                bigcup->setVisible(true);
                                
                                auto soda_down_up = Sprite::create("png/make2/soda_down.png");
                                soda_down_up->setAnchorPoint(Vec2::ZERO);
                                bigcup->addChild(soda_down_up, 10);
                                
                                //具体的果汁  根据上一步得到
                                auto soda_down_through = Sprite::create("png/make2/soda_up_blueberry.png");
                                soda_down_through->setAnchorPoint(Vec2::ZERO);
                                bigcup->addChild(soda_down_through, 10);
                                
                                auto bigcupUp = _operate->getNodeByName<Sprite*>("bigcupUp");
                                bigcupUp->setTexture("png/make2/soda_blueberry0.png");
                                
                                this->cupAction();
                            
                            }), NULL));
                        
                        }),NULL));
                        
                    
                    
                    }), NULL));
                    
                    
                    
                    
                }
                
            }),
                                                  NULL));
            
        }
        
        
    });

//    touchNode->addListners(ComponentTouchBegan, [=](Touch*,Event*){});
//    touchNode->addListners(ComponentTouchEnd, CC_CALLBACK_2(BlenderLayer::touchUp, this));
//    touchNode->addListners(ComponentTouchCancle, CC_CALLBACK_2(BlenderLayer::touchUp, this));

}
void MakeStep2Layer::cupAction()
{
    //增加节点
    auto cup = _operate->getNodeByName("bowl");
    
    //杯子
    auto cup_cover = Sprite::create("png/make2/cup_up.png");
    cup_cover->setAnchorPoint(Vec2::ZERO);
    cup->addChild(cup_cover);

    auto cup_shadow = Sprite::create("png/make2/cup_shadow.png");
    cup_shadow->setAnchorPoint(Vec2::ZERO);
    cup->addChild(cup_shadow, -1);
    
    cup->setVisible(true);
    ActionHelper::showBackInOut(cup, cup->getPosition(), ActionHelper::ShowDirection::show_from_left, [=](){
    
        ActionHelper::runMoveHideAction(_prompt, _operate->getPosinWord("bigcup"), _operate->getPosinWord("limit3"));
        auto bigcup = _operate->getNodeByName("bigcup");
        auto drag = createDraOperate(bigcup);
        drag->setNeedGravity(true);
        
        
    });

}

void MakeStep2Layer::startFall(LQComponent* fall, OperateListner* _lis){
    BaseMakeLayer::startFall(fall, _lis);
}
void MakeStep2Layer::fallEnd(LQComponent* fall, OperateListner* _lis){
    BaseMakeLayer::fallEnd(fall, _lis);
}



